package com.aaa.service.impl;

import com.aaa.dao.MovieDao;
import com.aaa.entity.Movie;
import com.aaa.entity.Product;
import com.aaa.service.MovieService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieDao movieDao;
    public TableData<Movie> page(String keyword, Integer page, Integer limit) {
        TableData<Movie> tableData=new TableData<Movie>();
        Integer offset=(page-1)*limit;
        List<Movie> data = movieDao.listPage(keyword, offset, limit);
        tableData.setData(data);
        Integer count=movieDao.count(keyword);
        tableData.setCount(count);
        return tableData;
    }
    public DefaultMsg saveOrupdate(Movie movie) {
        DefaultMsg defaultMsg=new DefaultMsg();
        Integer count;
        if (movie.getId()==null){
            count=movieDao.save(movie);
        }else{
            count=movieDao.update(movie);
        }
        if (count==0){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("失败");
        }
        return defaultMsg;
    }

    public DefaultMsg delete(Long id) {
        DefaultMsg defaultMsg=new DefaultMsg();
        Integer count=movieDao.delete(id);
        if (count==0){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("失败");
        }
        return defaultMsg;
    }
}
